package com.xja.controller;

import com.xja.entity.ContentType;
import com.xja.entity.CourseInfo;
import com.xja.entity.CoursePerson;
import com.xja.service.CourseInfoService;
import com.xja.service.CoursePersonService;
import com.xja.util.FileUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;

@Controller
@RequestMapping("/course")
public class CourseController {
    private Logger logger = LoggerFactory.getLogger(CourseController.class);
    @Reference(url = "dubbo://localhost:20881")
    private CourseInfoService courseInfoService;
    @Reference(url = "dubbo://localhost:20881")
    private CoursePersonService coursePersonService;

    @RequestMapping("/getCourses")
    /*获取全部课程*/
    public String getCourses(Model model){
        System.out.println("调用了获取全部课程信息的方法");
        /*更新课程的状态*/
        courseInfoService.updateStatus();
        /*获取课程的分类*/
        List<ContentType> contentTypeList = courseInfoService.getCourseType();
        /*获取课程*/
        List<CourseInfo> courseList = courseInfoService.getCourses();
        model.addAttribute("courseList",courseList);
        model.addAttribute("contentTypeList",contentTypeList);
        /*for (ContentType type : contentTypeList) {
            System.out.println(type.getTypename());
        }
        for (CourseInfo courseInfo : courseList) {
            System.out.println(courseInfo.toString());
        }*/
        return "course";
    }

    @RequestMapping("/getCourseById")
    public String getCourseById(Model model,HttpServletRequest request,Integer cid,Integer uid){
        System.out.println("调用了获取具体课程的方法");
        /*String c = request.getParameter("cid");
        System.out.println(c);*/
        System.out.println(uid);
        System.out.println(cid);
        CoursePerson coursePerson = coursePersonService.selectCourseByUid(uid,cid);
        CourseInfo course = courseInfoService.getCourseById(cid);
        model.addAttribute("coursePerson",coursePerson);
        model.addAttribute("course",course);
        return "course-details";
    }

    @RequestMapping("/addPerson")
    /*加入课程*/
    public String addPerson(Integer cid,Integer uid){
        /*System.out.println(cid);*/
        System.out.println(uid);
        System.out.println(coursePersonService);
        CoursePerson coursePerson = coursePersonService.selectCourseByUid(uid,cid);
        if (coursePerson==null){
            System.out.println("未报名");
            boolean flag = coursePersonService.addPerson(cid,uid);
            if(flag){
                System.out.println("报名成功");
                return "redirect:/course/getCourses";
            }
            System.out.println("报名失败");
            return "redirect:/course/getCourseById?cid="+cid;
        }
        System.out.println("已过报名");
        return "redirect:/course/getCourses";
    }

    @RequestMapping("/addInfo")
    public String addInfo(){
        System.out.println("用户添加课程");
        return "course-add";
    }

    @RequestMapping("/addCourse")
    /*添加课程*/
    public String addCourse(CourseInfo courseInfo, @RequestParam("file")MultipartFile file,HttpServletRequest request){
        System.out.println("调用了添加课程的方法！");
        logger.info("添加课程");
        /*try {
            if (file.isEmpty()) {
                return "文件为空";
            }
            String fileName = file.getOriginalFilename();
            String filePath = request.getServletContext().getRealPath("");
            String newImg = "\\images\\"  + fileName;
            System.out.println(newImg);
            String path = filePath + "\\images\\" + fileName;
            System.out.println(path);
            File dest = new File(path);
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            file.transferTo(dest);
            System.out.println(dest);
            courseInfo.setCourseImage(newImg);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        String path = null;
        try {
            path = FileUtil.getTransferToPath(file);
            courseInfo.setCourseImage(path);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        boolean flag = courseInfoService.addCourse(courseInfo);
        if(flag){
            return "redirect:/course/getCourses";
        }
        return "";
    }


}
